#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include "method_print.h"

int main(void)
{
    printHTTPResponseHeader();
    printHTMLHead();
	int n, i;
    char * data;
    n = atoi(getenv("CONTENT_LENGTH"));
    data = (char *)malloc(n + 2);
    memset(data, '\0', n + 2);
    for(i = 0; i < (n + 1); ++i)
    {
        data[i] = getchar();
    }
	char eMail[255];
	int pos = getValue(0, data, eMail);
	if(strcmp(eMail, "") == 0)
	{
		printf("<h1>You didn't enter eMail!</h1>\n");
		printHTMLEnd();
		free(data);
		return 1;
	}
	toLowerStr(eMail);
	char * filename = malloc(strlen(eMail) + 5);;
	strcpy(filename, eMail);
	strcat(filename, ".txt");
	FILE * File = fopen(filename, "rb");
	if(File == NULL)
	{
		printf("<h1>This email is not exist!</h1>\n");
		printHTMLEnd();
		free(data);
		return 1;
	}
	char lastName[255];
	char reEmail[255];
	char password[255];
	char sex[255];
	char day[255];
	char month[255];
	char year[255];
	
	getDataFromFile(File, lastName, eMail, reEmail, password, sex, month, day, year);

    printHTMLHeaderLine();
    printHTMLInputData(lastName, eMail, reEmail, password);
    printHTMLSexSelector(sex);
    printHTMLBirthDaySelector(month, day, year);
    printHTMLFooter();
    
    printHTMLEnd();
    
    return 0;
}